var indexNumber = "";
var app = getApp();
var shareTickets = "";
var rank
Page({
  data: {
    authbg: getApp().globalData.source + "red_packet/bg.png",
    authtitle: getApp().globalData.source + "red_packet/title1.png",
    authbtn2: getApp().globalData.source + "red_packet/btn2.png",
    authdialog: getApp().globalData.source + "dialog/dialog.png",
    share: false,
    shareshow: false,
    navList: [{
        "text": "总排行榜"
      }
      // {
      //   "text": "群好友排行"
      // }
    ],
    indexNumber: 0,
    rankingList: [{
        "userId": 1,
        "sumTime": "--",
        "imageUrl": " ",
        "wxNickname": "--"
      },
      {
        "userId": 1,
        "sumTime": "--",
        "imageUrl": " ",
        "wxNickname": "--"
      },
      {
        "userId": 1,
        "sumTime": "--",
        "imageUrl": "",
        "wxNickname": "--"
      },
    ],

    rankingList2: [{
        "userId": 1,
        "sumTime": "??",
        "imageUrl": "../../images/income/wuming.png",
        "wxNickname": "星耀"
      },
      {
        "userId": 1,
        "sumTime": wx.getStorageSync("listen_time"),
        "imageUrl": wx.getStorageSync('userInfo').avatarUrl,
        "wxNickname": wx.getStorageSync('userInfo').nickName
      },
      {
        "userId": 1,
        "sumTime": "??",
        "imageUrl": "../../images/income/wuming.png",
        "wxNickname": "青铜"
      },
    ],
    dataList: [],
  },
  bindGetUserInfo: function (e) {
    getApp().authInfo(e);
  },
  clickItem: function(e) {
    // var that = this;
    // indexNumber = e.currentTarget.dataset.number;
    // that.setData({
    //   indexNumber: indexNumber
    // })
    // if (indexNumber == 1) {
    //   console.log("清空了datalist")
    //   this.setData({
    //     dataList: []
    //   })
    //   console.log("清空了datalist" + rank)
    //   if(rank!=undefined){
    //     if (rank.length > 0) {
    //       this.setData({
    //         rankingList: that.data.rankingList2,
    //         shareshow: false
    //       })
    //     } else {
    //       this.setData({
    //         rankingList: that.data.rankingList2,
    //         shareshow: true
    //       })
    //     }
    //   }else{
    //     this.setData({
    //       rankingList: that.data.rankingList2,
    //       shareshow: true
    //     })
    //   }
     

    // } else {
    //   this.setData({
    //     rankingList: that.data.rankingList2,
    //     shareshow: false
    //   })
    //   this.findIncome()
    // }

  },

  onLoad: function(options) {
    var vm = this
    var that = this
    wx.showShareMenu({
      withShareTicket: true
    })
  },
  findIncome: function() {
    var data = {
      "startPoint": 1,
      "maxPage": 10,
    }
    var that = this;
    app.requestLoading(app.globalData.https + "/hd/allListenRank",
      "get", data, "加载中...",
      function(res) {
        var have=false;
        for(var i=0;i<res.data.data.length;i++){
          if (res.data.data[i].userId==wx.getStorageSync("userId")){
            have=true;
            wx.setStorageSync("listen_time", Math.round(res.data.data[i].sumTime))
          }
        }
        if (!have){
          wx.setStorageSync("listen_time",0)
        }
        if (res.data.data.length == 1) {
          var data = that.data.rankingList;
          data[1] = res.data.data[0];
          that.setData({
            rankingList: data
          })
        } else if (res.data.data.length == 2) {
          var data = that.data.rankingList;
          data[1] = res.data.data[0];
          data[0] = res.data.data[1];
          that.setData({
            rankingList: data
          })
        } else if (res.data.data.length == 3) {
          var data = that.data.rankingList;
          data[1] = res.data.data[0];
          data[0] = res.data.data[1];
          data[2] = res.data.data[2];
          that.setData({
            rankingList: data
          })
        } else {
          var data = that.data.rankingList;
          var list = res.data.data;
          data[1] = res.data.data[0];
          data[0] = res.data.data[1];
          data[2] = res.data.data[2];
          list.shift();
          list.shift();
          list.shift();
          that.setData({
            rankingList: data,
            dataList: list
          })
        }
      },
      function(res) {}
    )
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function(e) {
    var vm = this
    var that = this
    wx.getStorage({
      key: 'scene',
      success: function(res) {
        console.log("获取scene成功" + res.data)
        if (res.data == 1044) {
          wx.getStorage({
            key: 'shareTicket',
            success: function(res2) {
              wx.getShareInfo({
                shareTicket: res2.data,
                success: function(res) {
                  var encryptedData = res.encryptedData;
                  var iv = res.iv;
                  wx.login({
                    success: function(res) {
                      var code = res.code;
                      console.log(code)
                      var userId = wx.getStorageSync("userId");
                      var data = {
                        'wxEncryptedData': encryptedData,
                        "wxIv": iv,
                        "wxCode": code
                      };
                      console.log(data)
                      //处理获取分享群信息  

                      getApp().requestLoading(getApp().globalData.https + "programResource/saveProgramShareGroup",
                        "get", data,"加载中...",
                        function(res) {
                          console.log("分享成功" + JSON.stringify(res))
                          var data = {
                            "gId": res.data
                          };
                          getApp().requestLoading(getApp().globalData.https + "hd/userGroupRank",
                            "get", data, "加载中...",
                            function(res) {
                              var rank_data = res.data;
                              rank = rank_data;
                              console.log("获取成功" + JSON.stringify(res))
                              wx.removeStorage({
                                key: 'scene',
                                success(res) {
                                  console.log(res.data)
                                }
                              })
                              wx.removeStorage({
                                key: 'shareTicket',
                                success(res) {
                                  console.log(res.data)
                                }
                              })
                              if (rank_data != undefined) {
                                if (rank_data != undefined) {
                                  if (rank_data.length > 0) {
                                    var have = false;
                                    for (var i = 0; i < rank_data.length; i++) {
                                      if (rank_data[i].userId == wx.getStorageSync("userId")) {
                                        have = true;
                                        wx.setStorageSync("listen_time", Math.round(rank_data[i].sumTime))
                                      }
                                    }
                                    if (!have) {
                                      wx.setStorageSync("listen_time", 0)
                                    }
                                    if (rank_data.length == 1) {
                                      var data = vm.data.rankingList2;

                                      data[1] = rank_data[0];
                                      that.setData({
                                        rankingList: data
                                      })
                                    } else if (rank_data.length == 2) {
                                      var data = vm.data.rankingList2;
                                      data[1] = rank_data[0];
                                      data[0] = rank_data[1];
                                      that.setData({
                                        rankingList: data
                                      })
                                    } else if (rank_data.length == 3) {
                                      var data = vm.data.rankingList2;
                                      data[1] = rank_data[0];
                                      data[0] = rank_data[1];
                                      data[2] = rank_data[2];
                                      that.setData({
                                        rankingList: data
                                      })
                                    } else {
                                      var data = vm.data.rankingList2;
                                      var list = rank_data;
                                      data[0] = rank_data[0];
                                      data[1] = rank_data[1];
                                      data[2] = rank_data[2];
                                      list.shift();
                                      list.shift();
                                      list.shift();
                                      that.setData({
                                        rankingList: data,
                                        dataList: list
                                      })
                                    }
                                    vm.setData({
                                      indexNumber: 1,
                                      shareshow: false
                                    })
                                  }
                                }
                              }
                              // wx.setStorageSync("rank", JSON.stringify(res.data))
                            },
                            function(res) {
                              console.log(res)
                            }
                          )
                        },
                        function(res) {
                          console.log(res)
                        }
                      )
                    }
                  })
                }
              })
            },
          })
        }else{
          if (vm.data.indexNumber != 1) {
            vm.findIncome()
          } 
        }
      },
      fail:function(res){
        console.log("走到获取场景失败这里 "+vm.data.indexNumber)
        rank=undefined;
        if (vm.data.indexNumber!=1){
          vm.findIncome()        
        }
      }
    })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    var that = this
    return {
      title: wx.getStorageSync("userInfo").nickName + '邀您一起听' + "《天天必听》",
      success(res) {
        //分享成功 
        shareTickets = res.shareTickets;
        console.log(shareTickets)
        console.log(JSON.stringify(shareTickets))
        if (shareTickets == undefined) { //执行个人分享保存 
          //分装提交数据 shareSource 栏目id shareStatus分享状态
          var datas = {
            programId: "0",
            shareSource: "0",
            //shareStatus: "1"
          }
          //暂时封印个人分享栏目保存记录TODO.....
          //that.singleGroupId(datas); //调取保存用户分享的栏目

        } else { //执行群分享保存
          console.log("进入群分享")
          var datas = {
            userId: wx.getStorageSync("userId"),
            programId: "0",
            shareSource: "1",
            //shareStatus: "1"
          }
          //暂时封印组分享栏目保存记录TODO.....
          that.getGroupID(datas); //调用获取群编号的方法
        }

      },
      fail(e) {
        console.log("fail")
      },
      //分享完成
      complete() {
        console.log("complete")
      }
    }
  },
  getGroupID: function(datas) { //获取分享群id
    wx.getShareInfo({
      shareTicket: shareTickets[0],
      success: function(res) {
        var encryptedData = res.encryptedData;
        var iv = res.iv;
        wx.login({
          success: function(res) {
            var code = res.code;
            console.log(code)
            var userId = datas.userId;
            var shareSource = datas.shareSource;
            var data = {
              'wxEncryptedData': encryptedData,
              "wxIv": iv,
              "wxCode": code
            };
            console.log(data)
            //处理获取分享群信息
            app.request(app.globalData.https + "programResource/saveProgramShareGroup",
              "get", data,
              function(res) {
                console.log("分享成功" + res)
              },
              function(res) {
                console.log(res)
              }
            )
          }
        })
      }
    })
  },
})